package com.campus.web.controller.management;


import com.alibaba.fastjson.JSON;
import com.campus.app.service.ClassRoomsService;
import com.campus.app.service.impl.FunctionServerImpl;
import com.campus.commons.entity.ClassRoom;
import com.campus.commons.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 教室表 前端控制器
 * </p>
 *
 * @author Rin
 * @since 2020-12-28
 */
@RestController
@RequestMapping("/class-room")
public class ClassRoomController {
    @Autowired
    private ClassRoomsService classRoomsService;

    /**
     * 查询所有
     *
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("/all")
    public Result<List<ClassRoom>> getAll(int page, int limit) {
        Result<List<ClassRoom>> all = classRoomsService.getAll(page, limit);

        return all;
    }


    /**
     * 支持模糊查询
     *
     * @param page
     * @param limit
     * @param classRoomName
     * @return
     */
    @PostMapping("/likeAll")
    public Result<List<ClassRoom>> getAllByLike(int page, int limit, String classRoomName,String classRoomType) {
        Result<List<ClassRoom>> allByLike = classRoomsService.getAllByLike(page, limit, classRoomName);
        return allByLike;
    }

    /**
     * 添加
     *
     * @param classRoom
     * @return
     */
    @PostMapping("/add")
    public Result add(ClassRoom classRoom) {
        Result add = classRoomsService.add(classRoom);
        return add;
    }

    /**
     * 修改
     *
     * @param classRoom
     * @return
     */
    @PostMapping("/upDate")
    public Result UpDate(ClassRoom classRoom) {

        Result result = classRoomsService.upDate(classRoom);
        return result;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @PostMapping("/del")
    public Result delete(Integer id) {
        Result del = classRoomsService.del(id);
        return del;
    }

    @RequestMapping("/existClassRoom")
    public int exitClassRoom(String roomName) {
        new FunctionServerImpl().init();
        return FunctionServerImpl.classRooms.getOrDefault(roomName.toUpperCase(), -1);
    }


    public static List<HashMap<String, Object>> getClassRooms() {
        return ClassRoom.classrooms.keySet().stream().map(s -> {
            HashMap<String, Object> sr = new HashMap<>();
            sr.put("classroomName", ClassRoom.classrooms.get(s));
            sr.put("id", s);
            return sr;
        }).collect(Collectors.toList());
    }
}


